import store from '@/store'
const permission = {
  inserted(el, vDir) {
    const btnPermissions = store.getters.btnPermissions
    if (vDir.value) {
      let hasPermission = true
      if (vDir.value.condition != undefined && vDir.value.condition === false) {
        hasPermission = false
      } else if (vDir.value.permission instanceof Array) {
        hasPermission = vDir.value.permission.some((value) => {
          return btnPermissions.includes(value)
        })
      } else {
        hasPermission = btnPermissions.includes(vDir.value.permission)
      }
      if (!hasPermission && el.parentNode) {
        el.parentNode.removeChild(el)
      }
    }
  }
}

export function setupPermissionDirective(app) {
  app.directive('permission', permission)
}